<?php

namespace Xuan\Controller;

class UserController extends CommonController {

    public function lists() {
        $sql = "1=1 AND id != 1";
        $keyword = trim(I('get.keyword'));
        if ($keyword) {
            $sql .= " AND name like '%" . $keyword . "%'";
        }
        $count = M('user')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count, C("pagenum"));
        $lists = M('user')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("keyword", $keyword);
        $this->display();
    }

    public function detail() {
        $id = I('get.id', '', 'int');
        if ($id > 0) {
            $detail = M("user")->where("id = " . $id . "")->find();
            if ($detail['state'] == 1) {
                $log = M("admin_log")->where("uid = " . $detail['id'] . "")->find();
                $this->assign("log", $log);
            }
            $this->assign("detail", $detail);
        }
        $this->display();
    }

    public function detail_post() {

        $id = I('post.id', '', 'int');
        $data['state'] = I('post.state');
        if ($id == 0) {
            $data['addtime'] = time();
        }
        $data['name'] = I('post.name');
        $pwd = I('post.pwd');
        if ($pwd) {
            $data['pwd'] = md5($pwd);
        }
        $data['email'] = I('post.email');
        $data['phone'] = I('post.phone');
        if ($id > 0) {
            M("user")->where("id = " . $id . "")->save($data);
            $this->success('修改成功！', session('QUERY_STRING'));
        } else {
            M("user")->add($data);
            $this->success('添加成功！', U('User/lists'));
        }
    }
     public function points() {
         $sql = "1=1";
         $keyword = trim(I('get.keyword'));
         $mtype =I('get.mtype');
        if ($keyword) {
           $uids = getKeyUids($keyword);
           if($uids){
               $sql .= " AND uid in (".$uids.")";
           }else{
               $sql .= " AND uid = -1";
           }
        }
        if ($mtype) {
            $sql .= " AND mtype = '".$mtype."'";
        }
        
        $count = M('log')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count,C("pagenum"));
        $lists = M('log')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('addtime desc')->select();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("mtype", $mtype);
        $this->assign("keyword", $keyword);
        $this->display();
    }
    public function accounts() {
        $lists = M('accounts')->order('ord ASC,id desc')->select();
        $this->assign("lists", $lists);
        $this->display();
    }

    public function accounts_detail() {
        $id = I('get.id', '', 'int');
        $detail = M("accounts")->where("id = " . $id . "")->find();
        $threeLogin = threeLogin();
        $this->assign("detail", $detail);
        $this->assign("threeLogin", $threeLogin);
        $this->display();
    }

    public function accounts_detail_post() {
        $id = I('post.id', '', 'int');
        $data['name'] = I('post.name');
        $data['key'] = I('post.key');
        $data['secret'] = I('post.secret');
        $data['code'] = I('post.code');
        $data['is_check'] = I('post.is_check');
        $data['ord'] = I('post.ord', '30', 'int');
        if ($id > 0) {
            M("accounts")->where("id = " . $id . "")->save($data);
            $this->success('修改成功！', U('User/accounts'));
        } else {
            M("accounts")->add($data);
            $this->success('添加成功！', U('User/accounts'));
        }
        clearTempFile();
    }

}

?>
